package com.hyperchain.service.Impl;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hyperchain.constant.LoginConstant;
import com.hyperchain.entity.DbAccount;
import com.hyperchain.entity.vo.UserInfo;
import com.hyperchain.mapper.AccountMapper;
import com.hyperchain.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AuthServiceImpl implements AuthService {

    private final AccountMapper accountMapper;
    private final StringRedisTemplate redisTemplate;

    @Override
    public String login(DbAccount account) {
        LambdaQueryWrapper<DbAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DbAccount::getUserName , account.getUserName());
        queryWrapper.eq(DbAccount::getPassword , account.getPassword());
        DbAccount user = accountMapper.selectOne(queryWrapper);
        if(Objects.isNull(user)){
            throw new RuntimeException("账号或密码有误，请重试");
        }
        // 将用户的数据放入上下文session中
        StpUtil.login(user.getId());
        SaSession session = StpUtil.getTokenSession();
        session.set(LoginConstant.USER_NAME , user.getUserName());
        session.set(LoginConstant.USER_ID , user.getId());
        //将生成的token存入到redis中
//        String uuid = UUID.randomUUID().toString(true);
//        redisTemplate.opsForValue().set(LoginConstant.TOKEN + uuid, StpUtil.getTokenValue());
        return StpUtil.getTokenValue();
    }

    @Override
    public Integer register(DbAccount account) {
        if(StrUtil.isBlank(account.getUserName())){
            throw new RuntimeException("账号不能为空");
        }
        if(StrUtil.isBlank(account.getPassword())){
            throw new RuntimeException("密码不能为空");
        }
        return accountMapper.insert(account);
    }

    @Override
    public Set<String> userRoleSet(Integer id) {
        Set<String> roleSet = new HashSet<>();
        if(DbAccount.isAdmin(id)){
            roleSet.add("admin");
            return roleSet;
        }
        List<UserInfo> userRoleAndPermission = accountMapper.getUserRoleAndPermission(id);
        if(CollectionUtils.isNotEmpty(userRoleAndPermission)){
            roleSet = userRoleAndPermission.stream()
                    .map(UserInfo::getRoleKey)
                    .collect(Collectors.toSet());
        }
        return roleSet;
    }

    @Override
    public Set<String> userPermissionSet(Integer id) {
        Set<String> permissionSet = new HashSet<>();
        if(DbAccount.isAdmin(id)){
            permissionSet.add("*:*");
            return permissionSet;
        }
        List<UserInfo> userRoleAndPermission = accountMapper.getUserRoleAndPermission(id);
        if(CollectionUtils.isNotEmpty(userRoleAndPermission)){
            permissionSet = userRoleAndPermission.stream()
                    .map(UserInfo::getPermission)
                    .collect(Collectors.toSet());
        }
        return permissionSet;
    }
}
